package com.komodo.island.gdx.dialog;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;
import com.komodo.island.gdx.Input;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.screen.Screen;

public class MainMenuDialog extends Dialog {

	private Button playButton;
	private Button puzzleButton;
	public Button soundButton;
	public Button soundButtonOff;

	public Dialog dialog;
	
	private Button creditButton;

	public MainMenuDialog(float width, float height, boolean stretch,
			Screen screen, Texture texture,KomodoIsland komodoIsland) {
		super(width, height, stretch, screen, texture, komodoIsland);
		// TODO Auto-generated constructor stub
		this.ID = Input.DIALOG_MAIN_MENU;
		dialog = this;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

		// initByName();
		// initByTexture(screen.texture);
		

		playButton = new Button("play", new TextureRegion(
				komodoIsland.dialogTexture,  351+200,71+28*2, 64, 64),
				new TextureRegion(komodoIsland.dialogTexture,  351+200,71+28*2,
						64, 64));
		puzzleButton = new Button("fact", new TextureRegion(
				komodoIsland.buttonTexture, 90 + 63, 200, 32, 32),
				new TextureRegion(komodoIsland.buttonTexture,
						90 + 63 + 32, 0, 32, 32));
		// if (Persistence.sound)
		soundButton = new Button("soundon", new TextureRegion(
				komodoIsland.buttonTexture, 480 + 100 + 102, 0, 128, 64));
		// else
		// soundButton = new Button("soundoff", new TextureRegion(
		// komodoIsland.buttonTexture, 480 + 100 + 102 + 128,
		// 0, 128, 64));

		soundButtonOff = new Button("soundoff", new TextureRegion(
				komodoIsland.buttonTexture, 480 + 100 + 102 + 64, 0,
				128, 64));
		
		creditButton = new Button("credit", new TextureRegion(
				komodoIsland.buttonTexture, 90 + 63 + 64 * 3, 200, 32,
				32), new TextureRegion(komodoIsland.buttonTexture, 90
				+ 63 + 64 * 3 + 32, 200, 32, 32));

	}

	@Override
	public void addElements() {
		// TODO Auto-generated method stub
		setBackground(480 * 3, 320 * 2, 480, 320);
		addActor(playButton);
		addActor(puzzleButton);
//		addActor(soundButton);
		if (Persistence.sound) {
			addActor(soundButton);
		} else {
			addActor(soundButtonOff);
		}
		
		addActor(creditButton);
	}

	@Override
	public void setThePosition() {
		soundButton.x = 480 - 60 - 10;
		soundButton.y = 320 - 60;
		soundButtonOff.x = soundButton.x;
		soundButtonOff.y = soundButton.y;

		puzzleButton.x = soundButton.x - puzzleButton.width - 30;
		puzzleButton.y = 320 - 32 - 10;

		creditButton.x = puzzleButton.x - creditButton.width - 30;
		creditButton.y = puzzleButton.y;

		playButton.x = 185;
		playButton.y = 80;

	}

	@Override
	public void setTheListener() {
		// TODO Auto-generated method stub
		playButton.clickListener = new ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				if (Persistence.sound)
					komodoIsland.mainSound.play(1);
				screen.komodoIsland
						.goToScreen(komodoIsland.playerScreen);
			}
		};

		puzzleButton.clickListener = new ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				
//				System.out.println("Puzzle");
				if (Persistence.sound)
					komodoIsland.dialogSound.play(1);
				screen.komodoIsland
						.goToScreen(komodoIsland.puzzleScreen);
			}
		};

		soundButton.clickListener = new ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				// komodoIsland.goToScreen(komodoIsland.playerScreen);
				// komodoIsland.goToScreen(komodoIsland.factScreen);
				if (Persistence.sound)
					komodoIsland.dialogSound.play(1);
				Persistence.toggleSound();
				dialog.clear();
				dialog.addElements();
				if (Persistence.sound){
					komodoIsland.main.play();
				}else{
					komodoIsland.main.stop();
				}
			
			}
		};

		soundButtonOff.clickListener = new ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				// komodoIsland.goToScreen(komodoIsland.playerScreen);
				// komodoIsland.goToScreen(komodoIsland.factScreen);
				Persistence.toggleSound();
				dialog.clear();
				dialog.addElements();
				if (Persistence.sound){
					komodoIsland.main.play();
				}else{
					komodoIsland.main.stop();
				}
			}
		};

		creditButton.clickListener = new ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				// komodoIsland.goToScreen(komodoIsland.playerScreen);
//				System.out.println("Credit");
				screen.komodoIsland
						.goToScreen(komodoIsland.creditScreen);
			}
		};

	}

}
